using UnityEngine;

namespace UnityX
{
    [DisallowMultipleComponent]
    [RequireComponent(typeof(Renderer))]
    [AddComponentMenu("Animation/Gradient In")]
    public class GradientIn : AnimationBehaviour
    {
        public Color color = new Color(1, 1, 1, 0);
        public AnimationValueType valueType = AnimationValueType.固定值;

        private Color m_BeginValue;
        private Color m_EndValue;
        private Renderer m_Renderer = null;

        protected override void Begin()
        {
            if (m_Renderer == null)
            {
                m_Renderer = GetComponent<Renderer>();
            }
            switch (valueType)
            {
                case AnimationValueType.固定值:
                    m_BeginValue = color;
                    break;
                case AnimationValueType.固定增加:
                    m_BeginValue = m_Renderer.material.color + color;
                    break;
                case AnimationValueType.倍数:
                    m_BeginValue = m_Renderer.material.color * color;
                    break;
            }
            m_EndValue = m_Renderer.material.color;
            m_Renderer.material.color = m_BeginValue;
        }
        protected override void End()
        {
            m_Renderer.material.color = m_EndValue;
        }
        protected override void Set(float t)
        {
            m_Renderer.material.color = Color.Lerp(m_BeginValue, m_EndValue, t);
        }
    }
}